package Eve.EveB;

import java.io.*;
import Eve.Eve;

public class EveB extends Eve{

    protected final static String    EVE_CIPHER_DIR  = ".\\eveB cipher text\\";

    public static void main(String[] args){
        System.out.println("EveB is started");
        String filename = "ulyss12.txt";
        try {
            if (args.length == 1)
                filename = args[0];
            CrackCipherText.loadPopularWords(filename);
            EveB e = new EveB();
            e.run();
        }
        catch (IOException e) {
            System.out.println("Fatal error when trying to load dictionary file: "+filename);
            System.out.println("To use another file: ");
            System.out.println("java -jar EveB.jar filename.txt");
        }

    }

    // Object region

    /**
     * check for new files to attack under the CIPHER_DIR     
     */
    public void run(){
        //noinspection InfiniteLoopStatement
        while(true){
            File dir = new File(CIPHER_DIR);
            String[] children = dir.list();
            for (String filename : children) {
                if (filename.endsWith(".ct") && !m_processedFiles.contains(filename)){                    
                    String name= filename.substring(0,filename.length()-3);
                    try {
                        copyFile(new File(CIPHER_DIR+"\\"+name+".ct"),new File(EVE_CIPHER_DIR+"\\"+name+".ct"));
                        char[] key;
                        key = crack(name);
                        if (key != null){
                            saveKey(name,key);
                            m_processedFiles.add(filename);
                        }
                    }
                    catch (IOException e) {                        
                        System.out.println("Thecnical problem to access "+filename+", will retry later");
                    }
                }
            }
            //noinspection EmptyCatchBlock
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {}
        }
    }

    /**
     * cracking the ciher text using statistics methos
     * @param filename          The file to crack.
     * @return the cracked key
     * @throws IOException      A problem accessing the file
     */
    public char[] crack(String filename) throws IOException {
        File cipherFile = new File(CIPHER_DIR+"\\"+filename+".ct");        
        if (!cipherFile.exists() || cipherFile.length() == 0)
            return null;
        System.out.println("Attacking file: " + filename+" by CipherTextOnly");
                 
        FileInputStream fis = new FileInputStream(cipherFile);
        int len = (int) cipherFile.length();
        char[] message = new char[len];
        int[] statistics = new int[N];
        int max = -1;
        char maxAppear='e';
        for (int i=0; i<len; i++){
            message[i] = (char) fis.read();
            if (message[i]>=START_SYMBOL && message[i]<=END_SYMBOL){
                statistics[message[i]-START_SYMBOL]++;
                if (max < statistics[message[i]-START_SYMBOL]){
                    max = statistics[message[i]-START_SYMBOL];
                    maxAppear = message[i];
                }
            }
        }
        fis.close();

        char[] res = CrackCipherText.crack(message,maxAppear);
        System.out.println("Done attacking file");
        return res;
    }
}
